package cn.ibobo.lease.admin.controller;

import cn.ibobo.lease.admin.service.impl.UserCouponServiceImpl;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.dto.UserCouponDTO;
import cn.ibobo.lease.common.vo.UserCouponVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 用户点金券领用表控制器
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */

@RestController
@Api(tags = "点金券记录/统计")
@RequestMapping("userCoupon")
public class UserCouponController{

    private static final Log LOGGER = LogFactory.getLog(UserCouponController.class);

    @Autowired
    private UserCouponServiceImpl service;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
	 * 用户点金券领用表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "会员点金券记录", response = UserCouponVO.class)
    @PostMapping("list4Page")
    public ResultVO<Object> list4Page(
            UserCouponVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            UserCouponDTO entityDTO = new UserCouponDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<UserCouponDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, UserCouponVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 商家点金券统计
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "商家点金券统计", response = UserCouponVO.class)
    @PostMapping("listBus4Page")
    public ResultVO<Object> listBus4Page(
            UserCouponVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            UserCouponDTO entityDTO = new UserCouponDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<UserCouponDTO> list = service.listBus4Page(entityDTO, pageNum, pageSize);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), PaginationVOUtil.getPaginationVO(list, UserCouponVO.class, pageNum));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 用户点金券领用表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "用户点金券领用表列表（无分页）", response = UserCouponVO.class)
//    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(UserCouponVO entity){
        try {
            UserCouponDTO entityDTO = new UserCouponDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<UserCouponDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, UserCouponVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 用户点金券领用表详细信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "用户点金券领用表详细信息", response = UserCouponVO.class)
//    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            UserCouponDTO entityDTO = service.queryOneByPk(id);
            UserCouponVO entity = new UserCouponVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存用户点金券领用表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "保存用户点金券领用表信息", response = ResultVO.class)
//    @PostMapping("save")
    public ResultVO<Object> save(UserCouponVO entity) {
        try {
            UserCouponDTO entityDTO = new UserCouponDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除用户点金券领用表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
//    @ApiOperation(value = "删除用户点金券领用表信息", response = ResultVO.class)
//    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}